package com.viknix.threadpool.manager.client.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.Callable;

/**
 * @Author: Dongqi
 * @Date: 2021/11/24 9:59
 * @Version 1.0
 * @Description:
 */
@Slf4j
public class TemplateUtils {

    public static void stringNotEmptyAndThenExecute(String source, Runnable runnable) {

        if (StringUtils.isNotEmpty(source)) {

            try {
                runnable.run();
            } catch (Exception e) {
                log.error("string empty and then execute cause an exception.", e);
            }
        }
    }

    public static String stringEmptyAndThenExecute(String source, Callable<String> callable) {

        if (StringUtils.isEmpty(source)) {
            try {
                return callable.call();
            } catch (Exception e) {
                log.error("string empty and then execute cause an exception.", e);
            }
        }
        return source.trim();
    }

    public static String stringBlankAndThenExecute(String source, Callable<String> callable) {

        if (StringUtils.isBlank(source)) {

            try {
                return callable.call();
            } catch (Exception e) {
                log.error("string empty and then execute cause an exception.", e);
            }
        }
        return source.trim();
    }
}
